﻿using System;
using System.Linq;
using System.Data.Linq;
using UniMana.Data;
using UniMana.Utils.Data;
using System.Data;
namespace UniMana.Data
{
    public static class Catalogue_Course_SchoolYear_TermData
    {
        /// <summary>
        /// Lấy Năm Học, Học Kỳ theo khóa và ngày hiện tại
        /// </summary>
        /// <param name="course_SchoolYear_Term"></param>
        /// <param name="CourseKey"></param>
        /// <param name="SchoolYear"></param>
        /// <param name="Term"></param>
        public static void GetSchoolYear_TermByCourse_Year(
            this Table<Catalogue_Course_SchoolYear_Term> course_SchoolYear_Term,
                                                int CourseKey,
                                                ref int SchoolYear,
                                                ref int Term)
        {
            var Query = (from CoSYT in course_SchoolYear_Term
                         where
                           CoSYT.DateBegin < UniMana.Data.SysDateTime.Now &&
                           CoSYT.DateEnd > UniMana.Data.SysDateTime.Now  &&
                           CoSYT.Catalogue_CourseKey == CourseKey
                         select CoSYT).Take(1);

            DataTable dt = new DataTable();
            dt = Query.ToDataTable();
            if (dt.Rows.Count > 0)
            {
                SchoolYear = Convert.ToInt32(dt.Rows[0][1].ToString());
                Term = Convert.ToInt32(dt.Rows[0][2].ToString());
            }
            else
            {
                SchoolYear =-1;
                Term = -1;
            }
        }
        /// <summary>
        /// Lấy Năm Học, Học Kỳ sau theo khóa và ngày hiện tại
        /// </summary>
        /// <param name="course_SchoolYear_Term"></param>
        /// <param name="CourseKey"></param>
        /// <param name="SchoolYear"></param>
        /// <param name="Term"></param>
        public static void GetSchoolYear_TermByCourse_Year_After(
            this Table<Catalogue_Course_SchoolYear_Term> course_SchoolYear_Term,
                                                int CourseKey,
                                                ref int SchoolYear,
                                                ref int Term)
        {
            var Query = (from CoSYT in course_SchoolYear_Term
                         where
                           CoSYT.Catalogue_CourseKey == CourseKey &&
                           CoSYT.DateBegin > UniMana.Data.SysDateTime.Now
                         orderby
                           CoSYT.Catalogue_SchoolYearKey,
                           CoSYT.Catalogue_TermKey
                         select CoSYT).Take(1);

            DataTable dt = new DataTable();
            dt = Query.ToDataTable();
            if (dt.Rows.Count > 0)
            {
                SchoolYear = Convert.ToInt32(dt.Rows[0][1].ToString());
                Term = Convert.ToInt32(dt.Rows[0][2].ToString());
            }
            else
            {
                SchoolYear = -1;
                Term = -1;
            }
        }
        /// <summary>
        /// Lấy Năm Học, Học Kỳ trước theo khóa và ngày hiện tại
        /// </summary>
        /// <param name="course_SchoolYear_Term"></param>
        /// <param name="CourseKey"></param>
        /// <param name="SchoolYear"></param>
        /// <param name="Term"></param>
        public static void GetSchoolYear_TermByCourse_Year_Befor(
            this Table<Catalogue_Course_SchoolYear_Term> course_SchoolYear_Term,
                                                int CourseKey,
                                                ref int SchoolYear,
                                                ref int Term)
        {
            var Query = (from CoSYT in course_SchoolYear_Term
                         where
                           CoSYT.DateEnd < UniMana.Data.SysDateTime.Now &&
                           CoSYT.Catalogue_CourseKey == CourseKey
                         orderby
                           CoSYT.Catalogue_SchoolYearKey descending,
                           CoSYT.Catalogue_TermKey descending
                         select CoSYT).Take(1);

            DataTable dt = new DataTable();
            dt = Query.ToDataTable();
            if (dt.Rows.Count > 0)
            {
                SchoolYear = Convert.ToInt32(dt.Rows[0][1].ToString());
                Term = Convert.ToInt32(dt.Rows[0][2].ToString());
            }
            else
            {
                SchoolYear = -1;
                Term = -1;
            }
        }
        /// <summary>
        /// lấy danh sách ngày tháng của năm học và học kỳ đã chọn
        /// </summary>
        /// <param name="course_SchoolYear_Term"></param>
        /// <param name="CourseKey"></param>
        /// <param name="SchoolYear"></param>
        /// <param name="Term"></param>
        public static void Get_dateBegin_End_term_Schoolyear( 
             this Table<Catalogue_Course_SchoolYear_Term> course_SchoolYear_Term,
             ref DateTime DateBegin,
             ref DateTime DateEnd,
             int SchoolYear = 0,
             int Term = 0)
        {
            DateBegin = (from c_sy_t in course_SchoolYear_Term
                         where  c_sy_t.Catalogue_SchoolYearKey == SchoolYear && 
                                c_sy_t.Catalogue_TermSchoolYearKey==Term
                          select new
                          {
                              c_sy_t.DateBegin
                          }).Take(1).Min(p => p.DateBegin).Value.Date;
            DateEnd = (from c_sy_t in course_SchoolYear_Term
                       where c_sy_t.Catalogue_SchoolYearKey == SchoolYear &&
                                c_sy_t.Catalogue_TermSchoolYearKey == Term
                           select new
                       {
                           c_sy_t.DateEnd
                       }).Take(1).Max(p => p.DateEnd).Value.Date;
        }
        /// <summary>
        /// lấy năm học và học kỳ
        /// </summary>
        /// <param name="nCouserSchoolYearTerm"></param>
        /// <param name="nTerm"></param>
        /// <param name="_iSchoolYear"></param>
        /// <param name="_iCouserKey"></param>
        /// <returns></returns>
        public static IQueryable GetTermByCouserAndSchoolYear(
            this Table<Catalogue_Course_SchoolYear_Term> nCouserSchoolYearTerm, 
            Table<Catalogue_Term> nTerm, 
            int _iSchoolYear, 
            int _iCouserKey)
        {
            var iQuery = from couserschoolyearterm in nCouserSchoolYearTerm
                         join term in nTerm on couserschoolyearterm.Catalogue_TermKey equals term.Catalogue_TermKey
                         where
                            couserschoolyearterm.Catalogue_CourseKey == _iCouserKey
                            && couserschoolyearterm.Catalogue_SchoolYearKey == _iSchoolYear
                         select term;
            return iQuery;
        }
        /// <summary>
        /// Get Data
        /// </summary>
        /// <param name="course_SchoolYear_Term"></param>
        /// <param name="CourseKey"></param>
        /// <param name="SchoolYear"></param>
        /// <param name="Term"></param>
        public static IQueryable GetBy_course_SchoolYear_Term(
            this Table<Catalogue_Course_SchoolYear_Term> course_SchoolYear_Term,
                                                int CourseKey,
                                                int SchoolYear,
                                                int Term)
        {            
            if (CourseKey != -1)
            {
                if (SchoolYear != -1)
                {
                    if (Term != -1)
                    {
                        return course_SchoolYear_Term.Where(Q => Q.Catalogue_CourseKey == CourseKey
                            && Q.Catalogue_SchoolYearKey == SchoolYear
                            && Q.Catalogue_TermKey == Term);
                    }
                    else
                    {
                        return course_SchoolYear_Term.Where(Q => Q.Catalogue_CourseKey == CourseKey
                            && Q.Catalogue_SchoolYearKey == SchoolYear);
                    }
                }
                else
                {
                    if (Term != -1)
                    {
                        return course_SchoolYear_Term.Where(Q => Q.Catalogue_CourseKey == CourseKey
                            && Q.Catalogue_TermKey == Term);
                    }
                    else
                    {
                        return course_SchoolYear_Term.Where(Q => Q.Catalogue_CourseKey == CourseKey);
                    }
                }
            }
            else
            {
                if (SchoolYear != -1)
                {
                    if (Term != -1)
                    {
                        return course_SchoolYear_Term.Where(Q => Q.Catalogue_SchoolYearKey == SchoolYear
                            && Q.Catalogue_TermKey == Term);
                    }
                    else
                    {
                        return course_SchoolYear_Term.Where(Q => Q.Catalogue_SchoolYearKey == SchoolYear);
                    }
                }
                else
                {
                    if (Term != -1)
                    {
                        return course_SchoolYear_Term.Where(Q => Q.Catalogue_TermKey == Term);
                    }
                    else
                    {
                        return course_SchoolYear_Term;
                    }
                }
            }
        }
    }
}